#!/usr/bin/python3.8.9
# -*- coding: utf-8 -*-

# @Author  : Tina Yu
# @Time    : 2021-7-21 20:44
"""
使用多个文件
"""


# FileNotFoundError
def count_words(filename):
    """计算一个文件包含多少个单词"""
    try:
        with open(filename, encoding='utf-8') as f:
            contents = f.read()
    except FileNotFoundError:
        print(f"对不起，文件{filename}不存在！")
    else:
        words = contents.split()
        num_words = len(words)
        print(f"共计{num_words}个词!")


filenames = ['programming.txt', 'alice.txt']
for filename in filenames:
    count_words(filename)


# 静默失败
def count_words(filename):
    """计算一个文件包含多少个单词"""
    try:
        with open(filename, encoding='utf-8') as f:
            contents = f.read()
    except FileNotFoundError:
        pass
    else:
        words = contents.split()
        num_words = len(words)
        print(f"共计{num_words}个词!")


filenames = ['programming.txt', 'alice.txt']
for filename in filenames:
    count_words(filename)
